<?php
/*******************************************************************************
 * $Date:: 29 May 2010 01:47:51												   $
 * $Revision::																   $
 * $Author:: Tuan Duong													   	   $
 * $Comment::															   	   $
 ******************************************************************************/

/**
 * Util for xml/html document
 * @author Tuan Duong
 * @version 1.0
 * @package AI
 */
class Bet_Util_Xml {

    /**
     * @var DOMDocument
     */
    private $xDoc;
    
    /**
     * @var DOMXPath
     */
    private $xPath;
    
    public function __construct($path = "") {
        $this->xDoc = new DOMDocument();
        if ("" != $path) {
            try {
                $this->xDoc->load($path);
            } catch (Exception $ex) {
                die("Please input invalid xml path");
            }           
        }
        $this->xPath = new DOMXPath($this->xDoc);
    }
    
    public function load($html) {
        return @$this->xDoc->loadHTML($html);
    }
    
    public function loadXml($xml) {
        return @$this->xDoc->loadXML($xml);
    }
    
    public function getValue($nodeName) {
        $arrNode = $this->xDoc->getElementsByTagName($nodeName);
        if (count($arrNode) <= 0) {
            return false;
        }
        $returnValue = "";
        foreach ($arrNode as $node) {
            $returnValue = $node->nodeValue;
            break;
        }
        return $returnValue;
    }
    
    public function getNode($nodeName) {
        $arrNode = $this->xDoc->getElementsByTagName($nodeName);
        if (count($arrNode) <= 0) {
            return false;
        }
        $returnNode = "";
        foreach ($arrNode as $node) {
            $returnNode = $node->nodeValue;
            break;
        }
        return $returnNode;
    }
    
    /**
     * @param string $nodeName
     * @param string $attribute
     * @param string $value
     * @return DOMElement
     */
    public function getNodeWithAttribute($nodeName, $attribute, $value) {
        $arrNode = $this->xDoc->getElementsByTagName($nodeName);
        if (count($arrNode) <= 0) {
            return false;
        }
        $returnNode = false;
        foreach ($arrNode as $key => $node) {
            if ($node->getAttribute($attribute) == $value) {
                $returnNode = $node;
            }               
        }
        return $returnNode;
    }
    
    public function getNodeValueByNodeName($nodeName, $xDoc = "") {
        if ("" == $xDoc) {
            $xDoc = $this->xDoc;
        }
        $nodeList = new DOMNodeList();
        $nodeList = $xDoc->getElementsByTagName($nodeName);
        $returnValue = "";
        foreach ($nodeList as $node) {
            $returnValue = $node->nodeValue;
            break;
        }
        return $returnValue;
    }

    public function getNodeAtributeByNodeName($nodeName, $attrName, $xDoc = "") {
        if ("" == $xDoc) {
            $xDoc = $this->xDoc;
        }
        $nodeList = new DOMNodeList();
        $nodeList = $xDoc->getElementsByTagName($nodeName);
        $returnValue = "";
        foreach ($nodeList as $node) {
            $returnValue = $node->getAttribute($attrName);
            break;
        }
        return $returnValue;
    }
    
    /**
     * Use xPath to get the firstnode with expr $query 
     * @param string $query
     * @return DOMElement
     */
    public function getFirstNode($query) {
        try {
            $nodeList = $this->xPath->query($query);
        } catch (Exception $ex) {
            Bet_Util::getBetLogger()->err($ex->getMessage());
            return false;
        }
        if (0 == $nodeList->length) {
            return false;
        }
        foreach ($nodeList as $node) {
            $returnedNode = $node;
            break;
        }
        return $returnedNode;
    }
    
    /**
     * @return DOMDocument
     */
    public function getXDoc() {
        return $this->xDoc;
    }
    
    /**
     * @return DOMXPath
     */
    public function getXPath() {
        return $this->xPath;
    }
}
 
